using UnityEngine;

public class RCCCarChange : MonoBehaviour
{
	private GameObject[] objects;

	private GameObject activeObject;

	private int activeObjectIdx;

	private Camera mainCamera;

	private bool selectScreen = true;

	public Vector3 cameraOffset = new Vector3(10f, -70f, 0f);

	private void Awake()
	{
		RCCCarControllerV2[] array = UnityEngine.Object.FindObjectsOfType<RCCCarControllerV2>();
		objects = new GameObject[array.Length];
		for (int i = 0; i < array.Length; i++)
		{
			objects[i] = array[i].gameObject;
		}
		GameObject[] array2 = objects;
		foreach (GameObject gameObject in array2)
		{
			gameObject.GetComponent<RCCCarControllerV2>().canControl = false;
			gameObject.GetComponent<RCCCarControllerV2>().runEngineAtAwake = false;
			gameObject.GetComponent<RCCCarControllerV2>().engineRunning = false;
		}
		mainCamera = UnityEngine.Object.FindObjectOfType<RCCCarCamera>().GetComponent<Camera>();
	}

	private void Update()
	{
		if (selectScreen)
		{
			mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, objects[activeObjectIdx].transform.position + -mainCamera.transform.forward * 10f + new Vector3(0f, 0.5f, 0f), Time.deltaTime * 5f);
			mainCamera.transform.rotation = Quaternion.Euler(cameraOffset);
			GetComponent<Camera>().fieldOfView = 50f;
		}
	}

	private void OnGUI()
	{
		if (selectScreen)
		{
			GUIStyle style = GUI.skin.GetStyle("Button");
			style.alignment = TextAnchor.MiddleCenter;
			if (GUI.Button(new Rect(Screen.width / 2 + 65, 100f, 120f, 50f), "Next"))
			{
				activeObjectIdx++;
				if (activeObjectIdx >= objects.Length)
				{
					activeObjectIdx = 0;
				}
			}
			if (GUI.Button(new Rect(Screen.width / 2 - 185, 100f, 120f, 50f), "Previous"))
			{
				activeObjectIdx--;
				if (activeObjectIdx < 0)
				{
					activeObjectIdx = objects.Length - 1;
				}
			}
			if (!GUI.Button(new Rect(Screen.width / 2 - 60, 100f, 120f, 50f), "Select"))
			{
				return;
			}
			selectScreen = false;
			objects[activeObjectIdx].GetComponent<RCCCarControllerV2>().canControl = true;
			objects[activeObjectIdx].GetComponent<RCCCarControllerV2>().KillOrStartEngine();
			GetComponent<RCCCamManager>().enabled = true;
			GetComponent<RCCCarCamera>().playerCar = objects[activeObjectIdx].transform;
			GetComponent<RCCCamManager>().cameraChangeCount = 5;
			GetComponent<RCCCamManager>().ChangeCamera();
			if ((bool)UnityEngine.Object.FindObjectOfType<RCCUIDashboardButton>())
			{
				RCCUIDashboardButton[] array = UnityEngine.Object.FindObjectsOfType<RCCUIDashboardButton>();
				for (int i = 0; i < array.Length; i++)
				{
					array[i].Check();
				}
			}
		}
		else if (GUI.Button(new Rect(Screen.width / 2 - 120, 100f, 240f, 50f), "Select Screen"))
		{
			selectScreen = true;
			objects[activeObjectIdx].GetComponent<RCCCarControllerV2>().canControl = false;
			objects[activeObjectIdx].GetComponent<RCCCarControllerV2>().engineRunning = false;
			GetComponent<RCCCamManager>().cameraChangeCount = 10;
			GetComponent<RCCCamManager>().ChangeCamera();
			GetComponent<RCCCamManager>().enabled = false;
			GetComponent<RCCCarCamera>().enabled = false;
			GetComponent<RCCCameraOrbit>().enabled = false;
			mainCamera.transform.rotation = Quaternion.Euler(cameraOffset);
			GetComponent<Camera>().fieldOfView = 50f;
		}
	}
}
